package suites.page {
	import suites.stub.AsyncRequestPageStubEvent;

	import org.flexunit.asserts.assertEquals;
	import org.splink.deepsplink.boot.IDeepsplinkFacade;
	import org.splink.deepsplink.page.params.PageParameter;

	/**
	 * @author Max Kugland
	 */
	public class AsyncGotoPageInterruptTest extends AsyncGotoPageBase {
		
		[Test (async)]
		public function navigateLevel1And2And3Params() : void {
			handleAsync(2000);
			
			var facade : IDeepsplinkFacade = createFacade();
			getPage("1").register(AsyncRequestPageStubEvent.SHOW, function(e : AsyncRequestPageStubEvent) : void {
				clearHistory();
				asyncGotoPage(facade.navigationCommand, "8", [new PageParameter("param1", "hello")]);
			});
			
			getPage("6").register(AsyncRequestPageStubEvent.INIT, function(e : AsyncRequestPageStubEvent) : void {
				assertEquals("hide-1", _history[0]);
				assertEquals("params-root-1", _history[1]);
				assertEquals("params-5-1", _history[2]);
				assertEquals("initialize-5", _history[3]);
				assertEquals("show-5", _history[4]);
				assertEquals("params-6-1", _history[5]);
				assertEquals(6, _history.length);
				asyncGotoPage(facade.navigationCommand, "6", [new PageParameter("param1", "hello"), new PageParameter("param2", "world")]);
			});
			
			getPage("6").register(AsyncRequestPageStubEvent.SHOW, function(e : AsyncRequestPageStubEvent) : void {
				assertEquals("params-root-2", _history[0]);
				assertEquals("params-5-2", _history[1]);
				assertEquals("params-6-2", _history[2]);
				assertEquals("initialize-6", _history[3]);				assertEquals("show-6", _history[4]);
				assertEquals(5, _history.length);
				asyncGotoPage(facade.navigationCommand, "7", [new PageParameter("param1", "hello")]);
			});
		
			getPage("7").register(AsyncRequestPageStubEvent.SHOW, function(e : AsyncRequestPageStubEvent) : void {
				assertEquals("params-root-1", _history[0]);
				assertEquals("params-5-1", _history[1]);
				assertEquals("params-6-1", _history[2]);
				assertEquals("params-7-1", _history[3]);
				assertEquals("initialize-7", _history[4]);
				assertEquals("show-7", _history[5]);
				assertEquals(6, _history.length);
				asyncGotoPage(facade.navigationCommand, "3", [new PageParameter("param1", "hello"), new PageParameter("param2", "world")]);
			});
			
			getPage("5").register(AsyncRequestPageStubEvent.HIDE, function(e : AsyncRequestPageStubEvent) : void {
				assertEquals("hide-7", _history[0]);
				assertEquals("hide-6", _history[1]);				assertEquals("hide-5", _history[2]);
				assertEquals(3, _history.length);
				asyncGotoPage(facade.navigationCommand, "4", [new PageParameter("param1", "hello")]);
			});
			
			getPage("5").register(AsyncRequestPageStubEvent.FINALIZE, function(e : AsyncRequestPageStubEvent) : void {
				assertEquals("params-root-1", _history[0]);
				assertEquals("params-2-1", _history[1]);
				assertEquals("initialize-2", _history[2]);
				assertEquals("show-2", _history[3]);
				assertEquals("params-4-1", _history[4]);
				assertEquals("initialize-4", _history[5]);
				assertEquals("show-4", _history[6]);
				assertEquals("finalize-7", _history[7]);
				assertEquals("finalize-6", _history[8]);				assertEquals("finalize-5", _history[9]);				assertEquals(10, _history.length);				asyncGotoPage(facade.navigationCommand, "1");
			});
			
			getPage("2").register(AsyncRequestPageStubEvent.FINALIZE, function(e : AsyncRequestPageStubEvent) : void {
				assertEquals("hide-4", _history[0]);
				assertEquals("hide-2", _history[1]);
				assertEquals("params-root-0", _history[2]);
				assertEquals("initialize-1", _history[3]);
				assertEquals("show-1", _history[4]);
				assertEquals("finalize-4", _history[5]);
				assertEquals("finalize-2", _history[6]);
				assertEquals(7, _history.length);
				complete();
			});
		}		
	}
}
